﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Input;
using System.Xml.Serialization;
using Antioch.Base;
using Microsoft.Xna.Framework.Content;


namespace Antioch
{
	partial class ScreenGame : Screen
	{
		
#if !XBOX
        bool ConsoleOpen = false;
        List<String> ConsoleBuffer = new List<string>();
        String ConsoleInput = "";
        XnaTextInput.TextInputHandler TextInputHandler;
        SpriteFont ConsoleFont;
        String ValidCommandChars = " 0123456789_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ./-";

        void AddOutput(String str)
        {
            ConsoleBuffer.Add(str);
            if (ConsoleBuffer.Count > 8) ConsoleBuffer.RemoveAt(0);
        }
#endif

		private  void InitializeKeyboard()
		{
#if !XBOX
            ConsoleFont = Content.Load<SpriteFont>("Antioch.Content/console");
            TextInputHandler = new XnaTextInput.TextInputHandler(Screen.Parent.Window.Handle);
            TextInputHandler.KeyPress += (A, B) => { OnKeyPress(B.KeyChar); };
#endif
		}

#if !XBOX
        void OnKeyPress(Char key)
        {
            if (!ConsoleOpen && key == '`')
            {
                ConsoleOpen = true;
                return;
            }

            if (ConsoleOpen)
            {
                if (key == '`')
                {
                    ConsoleOpen = false;
                    return;
                }
                if (ValidCommandChars.Contains(key))
                {
                    ConsoleInput += key;
                    return;
                }
                if (key == (char)Keys.Back)
                {
                    if (ConsoleInput.Length >= 1) ConsoleInput = ConsoleInput.Substring(0, ConsoleInput.Length - 1);
                    return;
                }
                if (key == (char)Keys.Enter)
                {
                    var Parms = ConsoleInput.Split(new char[1] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    if (Parms.Length == 0) return;

                    if (String.Compare("load", Parms[0], true) == 0)
                    {
                        if (Parms.Length <= 1) { AddOutput("load map_name [node to fire]"); return; }
                        GameState.BeginChangeMap(Parms[1], Parms.Length == 3 ? Parms[2] : "");
                    }
                    else if (String.Compare("list", Parms[0], true) == 0)
                    {
                        if (Parms.Length == 2)
                        {
                            Jemgine.Engine.Tweakable Tweakable =
                                Jemgine.Engine.Tweakable.FindTweakable(Parms[1], World);
                            if (Tweakable == null)
                                AddOutput("Tweakable not found.");
                            else
                                AddOutput(Tweakable.GetValue());
                        }
                    }
                    else if (String.Compare("set", Parms[0], true) == 0)
                    {
                        if (Parms.Length != 3) { AddOutput("set variable value"); return; }
                        Jemgine.Engine.Tweakable Tweakable =
                            Jemgine.Engine.Tweakable.FindTweakable(Parms[1], World);
                        if (Tweakable == null)
                            AddOutput("Tweakable not found.");
                        else
                            Tweakable.SetValue(Parms[2]);
                    }
                }

            }
        }
#endif

        private bool SuppressUpdate()
        {
#if !XBOX
            return ConsoleOpen;
#else
            return false;
#endif
        }

        private void DrawConsole()
        {
#if !XBOX
            if (ConsoleOpen)
            {
                SpriteBatch.Begin();
                Vector2 Pos = Vector2.Zero;
                foreach (var Str in ConsoleBuffer)
                {
                    SpriteBatch.DrawString(ConsoleFont, Str, Pos, Color.White);
                    Pos.Y += ConsoleFont.LineSpacing;
                }
                SpriteBatch.DrawString(ConsoleFont, ConsoleInput + "_", Pos, Color.Red);
                SpriteBatch.End();
            }
#endif

		}

	}
}
